package audiobeatz;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.JButton;

import audio.SInstrument;
import audio.WavePlayer;

public class ButtonListener implements MouseListener {

    private JButton button;
    private AudioBeatz callback;

    public ButtonListener(JButton b, AudioBeatz callback) {
	this.button = b;
	this.callback = callback;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
	// not needed
    }

    @Override
    public void mouseEntered(MouseEvent e) {
	// not needed
    }

    @Override
    public void mouseExited(MouseEvent e) {
	// not needed
    }

    @Override
    public void mousePressed(MouseEvent e) {
	JButton sourceButton = (JButton) e.getSource();
	if (button.getText().equals(sourceButton.getText())) {
	    button.setBackground(Color.DARK_GRAY);
	    // Play it
	    new WavePlayer(SInstrument.getInstrument().getFolder() + File.separator + SInstrument.getInstrument().getFilename() + button.getText() + ".wav").start();

	    // Store it as recorded data, but only if we're
	    // recording
	    if (callback.isRecording() && !callback.isPaused())
		callback.addTimedChord(button.getText());
	}
    }

    @Override
    public void mouseReleased(MouseEvent e) {
	JButton sourceButton = (JButton) e.getSource();
	if (button.getText().equals(sourceButton.getText())) {
	    if (button.getText().contains("Bb") || button.getText().contains("#"))
		button.setBackground(Color.BLACK);
	    else
		button.setBackground(Color.WHITE);
	}
    }
}